package com.ff.xyh.front.service.impl;

import com.ff.xyh.common.entity.result.ResultCode;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.userfront.FrontUser;
import com.ff.xyh.common.entity.userfront.UserFavorite;
import com.ff.xyh.common.entity.userfront.UserRestricted;
import com.ff.xyh.common.utils.JwtUtils;
import com.ff.xyh.feign.client.minio.MinioClient;
import com.ff.xyh.feign.client.userfront.*;
import com.ff.xyh.front.service.UserFeignService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Auther FF
 * @Date 2022年 12月 06日
 * @Time 02:06
 */
@Service
public class UserFeignServiceImpl implements UserFeignService {

    @Resource
    private FrontUserClient frontUserClient;

    @Resource
    private FrontBlackerClient frontBlackerClient;

    @Resource
    private UserFavoriteClient userFavoriteClient;

    @Resource
    private UserFollowerClient userFollowerClient;

    @Resource
    private UserRestrictedClient userRestrictedClient;

    @Resource
    private MinioClient minioClient;


    /**
     * 登录
     */
    @Override
    public ResultObject login(
            String userToken, String password) {
        return frontUserClient.login(userToken, password);
    }

    @Override
    public ResultObject loginWithToken(String token) {
        return frontUserClient.loginWithToken(token);
    }

    /**
     * 注册
     */
    @Override
    public ResultObject register(
            FrontUser frontUser) {
        boolean hasUsername = (boolean) hasUserUsername(frontUser.getEmail()).getData().get("data");
        if (hasUsername) {
            return ResultObject.error().message("用户名已被注册");
        }
        boolean hasUserSno = (boolean) hasUserSno(frontUser.getEmail()).getData().get("data");
        if(hasUserSno) {
            return ResultObject.error().message("学号已被注册");
        }
        boolean hasUserEmail = (boolean) hasUserEmail(frontUser.getEmail()).getData().get("data");
        if (hasUserEmail) {
            return ResultObject.error().message("邮箱已被注册");
        }
        boolean hasUserPhone = (boolean) hasUserPhone(frontUser.getEmail()).getData().get("data");
        if (hasUserPhone) {
            return ResultObject.error().message("手机号已被注册");
        }
        return frontUserClient.register(frontUser);
    }

    /**
     * 上传头像 如果是第二次上传 则需要在请求头avatar中加入上次上传返回的路径
     */
    @Override
    public ResultObject uploadAvatar(
            MultipartFile multipartFile,
            HttpServletRequest request) {
        String url = request.getHeader("avatar");
        String uuid;
        // 没有url则生成uuid 有就截取出uuid
        if (StringUtils.isEmpty(url)) {
            uuid = UUID.randomUUID().toString();
        } else {
            // url: /files/img-files/avatar/xxx/xxx.png
            String[] strings = url.split("/");
            uuid = strings[strings.length-2];
        }
        return minioClient.uploadAvatar(multipartFile, uuid);
    }

    /**
     * 获取当前登录的用户id, 昵称, 头像, 性别, token 需要在请求中携带token
     */
    @Override
    @GetMapping("current")
    public ResultObject getCurrentUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        System.out.println(token);
        if (StringUtils.isEmpty(token)) {
            return ResultObject.error().message("用户未登录");
        } else {
            String userId = JwtUtils.getSubjectFromToken(token);
            return frontUserClient.getUserBaseInfoById(userId);
        }
    }

    /**
     * 通过id获取用户信息，仅测试用
     */
    @Override
    public ResultObject getUserById(
            String id) {
        return frontUserClient.getUserById(id);
    }

    /**
     * 判断 用户名 是否已被注册
     */
    @Override
    public ResultObject hasUserUsername(
            String username) {
        return frontUserClient.hasUserUsername(username);
    }

    /**
     * 判断 学号 是否已被注册
     */
    @Override
    public ResultObject hasUserSno(
            String sno) {
        return frontUserClient.hasUserSno(sno);
    }

    /**
     * 判断 邮箱 是否已被注册
     */
    @Override
    public ResultObject hasUserEmail(
            String email) {
        return frontUserClient.hasUserEmail(email);
    }

    /**
     * 判断 手机号 是否已被注册
     */
    @Override
    public ResultObject hasUserPhone(
            String phone) {
        return frontUserClient.hasUserPhone(phone);
    }

    /**
     * 更新用户信息，无法修改密码
     */
    @Override
    public ResultObject updateUser (
            FrontUser frontUser) {
        return frontUserClient.updateUser(frontUser);
    }

    /**
     * 修改密码 需要在请求头中传入token
     */
    @Override
    public ResultObject updateUserPasswordWithToken (
            String password,
            HttpServletRequest request) {
        if (StringUtils.isEmpty(password))
            return ResultObject.error().message("密码为空");
        else
            return frontUserClient.updateUserPasswordWithToken(password, request);
    }


    /**
     * 根据id删除用户
     */
    @Override
    public ResultObject deleteUserById (
            String id) {

        if (frontBlackerClient.deleteUserBlackById(id).getCode() != ResultCode.SUCCESS_CODE)
            return ResultObject.error().message("Delete UserBlacker Failed");
        if (userFavoriteClient.deleteUserFavoriteById(id).getCode() != ResultCode.SUCCESS_CODE)
            return ResultObject.error().message("Delete UserFavorite Failed");
        if (userFollowerClient.deleteUserFollowById(id).getCode() != ResultCode.SUCCESS_CODE)
            return ResultObject.error().message("Delete UserFollower Failed");
        return frontUserClient.deleteUserById(id);
    }


    /**
     * 用户拉黑
     */
    @Override
    public ResultObject insertUserBlack(
            String id, String blackerId ) {
//        Map<String, Object> data1 = frontUserClient.getUserById(id).getData();
//        Map<String, Object> data2 = frontUserClient.getUserById(blackerId).getData();
        if (frontUserClient.getUserById(id).getCode() == ResultCode.SUCCESS_CODE
                && frontUserClient.getUserById(blackerId).getCode() == ResultCode.SUCCESS_CODE) {
            return frontBlackerClient.insertUserBlack(id, blackerId);
        } else {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        }
    }

    /**
     * 取消拉黑
     */
    @Override
    public ResultObject deleteUserBlack(
            String id, String blackerId) {
        return frontBlackerClient.deleteUserBlack(id, blackerId);
    }

    /**
     * 用户收藏
     */
    @Override
    public ResultObject insertUserFavorite(
            String id, String postId, String postType ) {

        UserFavorite userFavorite = new UserFavorite();
        return userFavoriteClient.insertUserFavorite(userFavorite);
    }

    /**
     * 取消收藏
     * @param id
     * @param postId
     * @param postType
     * @return
     */
    @Override
    public ResultObject deleteUserFavorite(
            String id, String postId, String postType ) {
//        public ResultObject resultUser = frontUserClient.getUserById(id);
//        FrontUser user = (FrontUser) resultUser.getData().get("data");

        return userFavoriteClient.deleteUserFavorite(id, postId, postType);
    }
    /**
     * 用户关注
     */
    @Override
    public ResultObject insertUserFollow(
            String id, String followerId ) {
        if (frontUserClient.getUserById(id) != null && frontUserClient.getUserById(followerId) != null) {

        }
        return userFollowerClient.insertUserFollow(id, followerId);
    }

    /**
     * 取消关注
     */
    @Override
    public ResultObject deleteUserFollow(
            String id, String followerId ) {
        return userFollowerClient.deleteUserFollow(id, followerId);
    }

    /**
     * 根据用户id 取消所有关注信息
     */
    @Override
    public ResultObject deleteUserFollowById(
            String id ) {
        return userFollowerClient.deleteUserFollowById(id);
    }

    /**
     * 用户封号
     */
    @Override
    public ResultObject userRestrict(UserRestricted userRestricted) {
        return userRestrictedClient.userRestrict(userRestricted);
    }

    /**
     * 解除封号
     * @param userRestricted
     * @return
     */
    @Override
    public ResultObject userRestrictDelete(UserRestricted userRestricted) {
        return userRestrictedClient.userRestrictDelete(userRestricted);
    }

    @Override
    public ResultObject getUserBaseById(String id) {
        return frontUserClient.getUserBaseInfoById(id);
    }

    @Override
    public ResultObject getUserByOpenid(String openid) {
        return frontUserClient.getUserByOpenid(openid);
    }
}
